﻿using File_Encription;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace File_Encription
{
    
    [TestClass()]
    public class FileEncripionTest
    {

        [TestMethod()]
        public void EncryptionDecryptionFileTest()
        {
            string filename = @"C:\Users\User\Documents\visual studio 2010\"+
                @"Projects\Exam_DelagatesRegexStringsInputOutput\File Encription\bin\Debug\example.bin";
            FileEncripion_Accessor.EncryptFile(filename , "encrypted.bin");
              byte[] expected = new byte[] 
                {
                    0xf5, 0xa8 ,0x22 , 0xc3, 0xfe, 0x34, 0x56, 0x62
                };
              FileEncripion_Accessor.DecryptFile("encrypted.bin", "decrypted.bin");

              FileStream decrypedFile = File.Open("decrypted.bin", FileMode.Open);
              using (decrypedFile)
              {
                  byte[] buffer = new byte[decrypedFile.Length];
                  decrypedFile.Read(buffer, 0, buffer.Length);

                  Assert.AreEqual(buffer.Length, expected.Length);
                  for (int i = 0; i < buffer.Length; i++)
                  {
                      Assert.AreEqual(buffer[i], expected[i]);
                  }
              }
        }

        [TestMethod()]
        [DeploymentItem("File Encription.exe")]
        public void RotateBytesLeftWhenBytesAreExactly5Test()
        {
            byte[] bytes = new byte[]
            {
                0xF5, 0xA8 ,0x22 , 0xC3, 0xFE, 
            };

            byte[] expected = new byte[]
            {
                 0xA8 ,0x22 , 0xC3, 0xFE,0xF5
            };

            int bufferSize = 5; 
            FileEncripion_Accessor.RotateBytesLeft(bytes, bufferSize);

            for (int i = 0; i < bufferSize; i++)
            {
                Assert.AreEqual(expected[i], bytes[i]);
            }
        }

        [TestMethod()]
        [DeploymentItem("File Encription.exe")]
        public void RotateBytesLeftWhenBytesAreNotExactly5Test()
        {
            byte[] bytes = new byte[]
            {
                 0x34, 0x56, 0x62 
            };

            byte[] expected = new byte[]
            {
                  0x56, 0x62 ,0x34
            };

            int bufferSize = 3;
            FileEncripion_Accessor.RotateBytesLeft(bytes, bufferSize);

            for (int i = 0; i < bufferSize; i++)
            {
                Assert.AreEqual(expected[i], bytes[i]);
            }
        }

        [TestMethod()]
        [DeploymentItem("File Encription.exe")]
        public void RotateBytesRightWhenBytesAreExactly5Test()
        {
            byte[] bytes = new byte[]
            {
                 0xF5, 0xA8 ,0x22 , 0xC3, 0xFE
            };

            byte[] expected = new byte[]
            {
                  0xFE, 0xF5, 0xA8 ,0x22 , 0xC3
            };

            int bufferSize = 5;
            FileEncripion_Accessor.RotateBytesRight(bytes, bufferSize);

            for (int i = 0; i < bufferSize; i++)
            {
                Assert.AreEqual(expected[i], bytes[i]);
            }
        }

        [TestMethod()]
        [DeploymentItem("File Encription.exe")]
        public void RotateBytesRightWhenBytesAreNotExactly5Test()
        {
            byte[] bytes = new byte[]
            {
                 0x34, 0x56, 0x62
            };

            byte[] expected = new byte[]
            {
                0x62, 0x34, 0x56,  
            };

            int bufferSize = 3;
            FileEncripion_Accessor.RotateBytesRight(bytes, bufferSize);

            for (int i = 0; i < bufferSize; i++)
            {
                Assert.AreEqual(expected[i], bytes[i]);
            }
        }
    }
}
